package cn.cyh.queue_service.controller;

import cn.cyh.queue_service.common.Result;
import cn.cyh.queue_service.common.ResultCode;
import cn.cyh.queue_service.model.dto.PageRequest;
import cn.cyh.queue_service.model.dto.PageResult;
import cn.cyh.queue_service.model.dto.ServiceTypeRequest;
import cn.cyh.queue_service.model.vo.ServiceTypeVO;
import cn.cyh.queue_service.service.ServiceTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 业务类型管理控制器
 */
@RestController
@RequestMapping("/service-types")
@Tag(name = "业务类型管理", description = "业务类型管理相关接口")
@Slf4j
@RequiredArgsConstructor
public class ServiceTypeController {

    private final ServiceTypeService serviceTypeService;
    
    /**
     * 分页获取业务类型列表
     *
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @param name 业务类型名称（可选）
     * @param status 状态（可选）
     * @return 业务类型列表分页结果
     */
    @GetMapping("getList")
    @Operation(summary = "分页获取业务类型列表", description = "分页获取业务类型列表，支持按名称和状态筛选")
    public Result<PageResult<ServiceTypeVO>> getServiceTypeList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "业务类型名称") @RequestParam(required = false) String name,
            @Parameter(description = "状态：0-禁用, 1-启用") @RequestParam(required = false) Integer status) {
        
        log.info("获取业务类型列表: pageNum={}, pageSize={}", pageNum, pageSize);
        
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);
        pageRequest.addFilter("name", name);
        pageRequest.addFilter("status", status);
        
        PageResult<ServiceTypeVO> pageResult = serviceTypeService.getServiceTypeList(pageRequest);
        return Result.success(pageResult);
    }
    
    /**
     * 获取所有启用状态的业务类型
     *
     * @return 业务类型列表
     */
    @GetMapping("/active")
    @Operation(summary = "获取所有启用状态的业务类型", description = "获取所有启用状态的业务类型列表")
    public Result<List<ServiceTypeVO>> getActiveServiceTypes() {
        log.info("获取所有启用状态的业务类型");
        List<ServiceTypeVO> serviceTypes = serviceTypeService.getActiveServiceTypes();
        return Result.success(serviceTypes);
    }
    
    /**
     * 根据ID获取业务类型详情
     *
     * @param id 业务类型ID
     * @return 业务类型详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取业务类型详情", description = "根据业务类型ID获取业务类型详情")
    public Result<ServiceTypeVO> getServiceTypeById(@Parameter(description = "业务类型ID") @PathVariable Long id) {
        log.info("获取业务类型详情: id={}", id);
        ServiceTypeVO serviceTypeVO = serviceTypeService.getServiceTypeById(id);
        return Result.success(serviceTypeVO);
    }
    
    /**
     * 创建业务类型
     *
     * @param request 业务类型请求
     * @return 新创建的业务类型ID
     */
    @PostMapping
    @Operation(summary = "创建业务类型", description = "创建新的业务类型")
    @PreAuthorize("hasAnyRole('ADMIN', 'SUPERADMIN')")
    public Result<Long> createServiceType(@Valid @RequestBody ServiceTypeRequest request) {
        log.info("创建业务类型: {}", request.getName());
        Long id = serviceTypeService.createServiceType(request);
        return Result.success(id);
    }
    
    /**
     * 更新业务类型
     *
     * @param id 业务类型ID
     * @param request 业务类型请求
     * @return 更新结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新业务类型", description = "根据业务类型ID更新业务类型")
    @PreAuthorize("hasAnyRole('ADMIN', 'SUPERADMIN')")
    public Result<Boolean> updateServiceType(
            @Parameter(description = "业务类型ID") @PathVariable Long id,
            @Valid @RequestBody ServiceTypeRequest request) {
        
        log.info("更新业务类型: id={}", id);
        boolean result = serviceTypeService.updateServiceType(id, request);
        return Result.success(result);
    }
    
    /**
     * 删除业务类型
     *
     * @param id 业务类型ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除业务类型", description = "根据业务类型ID删除业务类型")
    @PreAuthorize("hasAnyRole('ADMIN', 'SUPERADMIN')")
    public Result<Boolean> deleteServiceType(@Parameter(description = "业务类型ID") @PathVariable Long id) {
        log.info("删除业务类型: id={}", id);
        boolean result = serviceTypeService.deleteServiceType(id);
        return Result.success(result);
    }
    
    /**
     * 更新业务类型状态
     *
     * @param id 业务类型ID
     * @param status 状态
     * @return 更新结果
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新业务类型状态", description = "根据业务类型ID更新业务类型状态")
    @PreAuthorize("hasAnyRole('ADMIN', 'SUPERADMIN')")
    public Result<Boolean> updateStatus(
            @Parameter(description = "业务类型ID") @PathVariable Long id,
            @Parameter(description = "状态：0-禁用, 1-启用") @RequestParam Integer status) {
        
        log.info("更新业务类型状态: id={}, status={}", id, status);
        
        if (status != 0 && status != 1) {
            return Result.failed(ResultCode.PARAM_ERROR, "状态值只能为0或1");
        }
        
        boolean result = serviceTypeService.updateStatus(id, status);
        return Result.success(result);
    }
} 